<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Allocation and Deallocation of Memory
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora084.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora086.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Allocation and Deallocation of Memory</H2>
<A NAME="@concepts182"></A>
Most languages permit dynamic memory allocation, among them C,
Pascal, Lisp, ML, SmallTalk, C++, Java, ADA.<BR>
<BR>
<A NAME="toc113"></A>
<H3> Explicit Allocation</H3>
<A NAME="@concepts183"></A>
We distinguish two types of allocation:
<UL>
<LI>
 a simple allocation reserving a block of memory of a certain
 size without concern of its contents;

<LI> an allocation combining the reservation of space with its initialization.
</UL>
The first case is illustrated by the function <TT>new</TT> in Pascal
or <TT>malloc</TT> in C. These return a pointer to a memory block
(i.e. its address), through which the value stored in memory can
be read or modified. The second case corresponds to the construction
of values in Objective CAML, Lisp, or in object-oriented languages.
Class instances in object-oriented languages are constructed by
combining <TT>new</TT> with the invocation of a constructor for the
class, which usually expects a number of parameters. In functional
languages, constructor functions are called in places where a
structural value (tuple, list, record, vector, or closure) is defined.<BR>
<BR>
<A NAME="@concepts184"></A><BR>
<BR>
Let's examine an example of value construction in Objective CAML. The
representation of values in memory is illustrated in
Figure&nbsp;<A HREF="book-ora085.html#fig-repr1">9.1</A>.
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<IMG SRC="book-ora030.gif">
</DIV>
<BR>
<DIV ALIGN=center>Figure 9.1: Memory representation of values.</DIV><BR>

<A NAME="fig-repr1"></A>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>


<PRE><BR># <B>let</B><CODE> </CODE>u<CODE> </CODE><CODE>=</CODE><CODE> </CODE><B>let</B><CODE> </CODE>l<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE>[</CODE><CODE>'c'</CODE>;<CODE> </CODE><CODE>'a'</CODE>;<CODE> </CODE><CODE>'m'</CODE><CODE>]</CODE><CODE> </CODE><B>in</B><CODE> </CODE>List.tl<CODE> </CODE>l<CODE> </CODE>;;<BR><CODE>val u : char list = ['a'; 'm']</CODE><BR># <B>let</B><CODE> </CODE>v<CODE> </CODE><CODE>=</CODE><CODE> </CODE><B>let</B><CODE> </CODE>r<CODE> </CODE><CODE>=</CODE><CODE> </CODE><TT>(</TT><CODE> </CODE><CODE>[</CODE><CODE>'z'</CODE><CODE>]</CODE><CODE> </CODE><CODE>,</CODE><CODE> </CODE>u<CODE> </CODE><TT>)</TT><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>in</B><CODE> </CODE><B>match</B><CODE> </CODE>r<CODE> </CODE><B>with</B><CODE> </CODE>p<CODE> </CODE>-&gt;<CODE> </CODE><TT>(</TT>fst<CODE> </CODE>p<TT>)</TT><CODE> </CODE><CODE>@</CODE><CODE> </CODE><TT>(</TT>snd<CODE> </CODE>p<TT>)</TT><CODE> </CODE>;;<BR><CODE>val v : char list = ['z'; 'a'; 'm']</CODE><BR>

</PRE>

A list element is represented by a tuple of two words, the first
containing a character and the second containing a pointer to the next
element of the list. The actual runtime representation differs
slightly and is described in the chapter on interfacing with C (see
page <A HREF="index.html#chap-IC">??</A>).<BR>
<BR>
The first definition constructs a value named <TT>l</TT> by allocating
a cell (constructor ::) for each element of the list
<CODE>[</CODE><CODE>'c'</CODE>;<CODE>'a'</CODE>;<CODE>'m'</CODE><CODE>]</CODE>. The global declaration <TT>u</TT>
corresponds to the tail of <TT>l</TT>. This establishes a sharing
relationship between <TT>l</TT> and <TT>u</TT>, i.e. between the
argument and the result of the function call to List.tl.<BR>
<BR>
Only the declaration <TT>u</TT> is known after the evaluation of this
first statement.<BR>
<BR>
The second statement constructs a list with only one element, then a
pair called <TT>r</TT> containing this list and the list <TT>u</TT>.
This pair is pattern matched 
and renamed <TT>p</TT> by the matching. Next, the first element of
<TT>p</TT> is concatenated with its second element, which creates a
value <CODE>[</CODE><CODE>'z'</CODE>;<CODE>'a'</CODE>;<CODE>'m'</CODE><CODE>]</CODE> tied to the global identifier
<TT>v</TT>. Notice that the result of <TT>snd</TT> (the list
<CODE>[</CODE><CODE>'a'</CODE>;<CODE>'m'</CODE><CODE>]</CODE>) is shared with its argument <TT>p</TT> whereas the
result of <TT>fst</TT> (the character <TT>'z'</TT>) is copied.<BR>
<BR>
In each case memory allocation is explicit, meaning that it is requested
by the programmer (by a language command or instruction).<BR>
<BR>


<H3> Note </H3> <HR>

Allocated memory stores information on the size of the object allocated
in order to be able to free it later.


<HR>

<BR>
<BR>
<A NAME="toc114"></A>
<H3> Explicit Reclamation</H3>
<A NAME="@concepts185"></A><A NAME="@concepts186"></A>
Languages with explicit memory reclamation possess a freeing operator (<TT>free</TT> in C or <TT>dispose</TT> in Pascal) that take the address (a
pointer) of the region to deallocate. Using the information stored at
allocation time, the program frees this region and may re-use it later.<BR>
<BR>
Dynamic allocation is generally used to manipulate data structures that
evolve, such as lists, trees etc.. Freeing the space occupied by
such data is not done in one fell swoop, but instead requires a
function to traverse the data. We call such functions destructors.<BR>
<BR>

Although correctly defining destructors
is not too difficult, their use is quite delicate.
In fact, in order to free the space occupied by a structure, it is
necessary to traverse the structure's pointers and apply the language's
freeing operator.
Leaving the responsibility of freeing memory to the programmer has the
advantage that the latter is sure of the actions taken.
However, incorrect use of these operators can cause an error during the
execution of the program.
The principal dangers of explicit memory reclamation are:
<UL>
<LI>
 dangling pointers: a memory region has been freed while there
 are still pointers pointing at it. If the region is reused, access
 to the region by these pointers risks being incoherent.

<LI> Inaccessible memory regions (a memory ``leak''): a memory region is
 still allocated, but no longer referenced by any pointer. There is no
 longer any possibility of freeing the region. There is a clear loss of memory.
</UL>
The entire difficulty with explicit memory reclamation is that of knowing
the lifetime of the set of values of a program.<BR>
<BR>
<A NAME="toc115"></A>
<H3> Implicit Reclamation</H3>
<A NAME="@concepts187"></A> Languages with implicit memory reclamation do not possess memory-freeing
operators. It is not possible for the programmer to free
an allocated value. Instead, an automatic
reclamation mechanism is engaged when a value is no longer referenced, or
at the time of an allocation failure, that is to say, when the heap is
full.<BR>
<BR>
An automatic memory reclamation algorithm is in some ways a global
destructor. This characteristic makes its design and
implementation more difficult than that of a destructor dedicated to a
particular data structure. But, once this difficulty is overcome, the
memory reclamation function obtained greatly enhances the safety of
memory management. In particular, the risk of dangling pointers
disappears.<BR>
<BR>
Furthermore, an automatic memory reclamation mechanism may bring good
properties to the heap:
<UL>
<LI>
 <EM>compaction</EM>: all the recovered memory belongs to a single
 block, thereby avoiding fragmentation of the heap, and allowing
 allocation of objects of the size of the free space on the heap;

<LI> <EM>localization</EM>: the different parts of the same value are
 close to one another from the point of view of memory address,
 permitting them to remain in the same memory pages during use,
and thereby avoiding their erasure from cache memory.
</UL>Design choices for a garbage collector must take certain
criteria and constraints into account:
<UL>
<LI>
 reclamation factor: what percentage of unused memory is available?

<LI> memory fragmentation: can one allocate a block the size of the
 free memory?

<LI> the slowness of allocation and collection;

<LI> what freedom do we have regarding the representation of values?
</UL>
In practice, the safety criterion remains primordial, and garbage
collectors find a compromise among the other constraints.<BR>
<BR>
<HR>
<A HREF="book-ora084.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora086.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
